﻿//-----------------------------------------------------------------------------------------------------------
// Wbfs Engine (WBFSe) Project by Omega Frost 
// http://wbfse.codeplex.com/
//
// WBFSe is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace WBFSe.IO
{
    /// <summary>
    /// Host to network helper
    /// </summary>
    static class Hton
    {
        /// <summary>
        /// Writes an int16 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteInt16(Byte[] b, int offset, short val)
        {
            BitConverter.GetBytes(IPAddress.HostToNetworkOrder(val)).CopyTo(b, offset);
        }

        /// <summary>
        /// Writes an int32 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteInt32(Byte[] b, int offset, int val)
        {
            BitConverter.GetBytes(IPAddress.HostToNetworkOrder(val)).CopyTo(b, offset);
        }

        /// <summary>
        /// Writes an int64 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteInt64(Byte[] b, int offset, long val)
        {
            BitConverter.GetBytes(IPAddress.HostToNetworkOrder(val)).CopyTo(b, offset);
        }

        /// <summary>
        /// Writes an uint16 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteUInt16(Byte[] b, int offset, ushort val)
        {
            WriteInt16(b, offset, (short)val);
        }

        /// <summary>
        /// Writes an uint32 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteUInt32(Byte[] b, int offset, uint val)
        {
            WriteInt32(b, offset, (int)val);
        }

        /// <summary>
        /// Writes an uint64 to a buffer converting to network order
        /// </summary>
        /// <param name="b">Buffer</param>
        /// <param name="offset">Offset in buffer</param>
        /// <param name="val">Value in host order</param>
        internal static void WriteUInt64(Byte[] b, int offset, ulong val)
        {
            WriteInt64(b, offset, (long)val);
        }
    }
}
